class Solution(object):
    def minCost(self, colors, neededTime):
        n = len(colors)
        left = 0
        right = 0
        time = 0
        while right < n:
            if colors[right] != colors[left]:
                if right - left > 1:
                    time += sum(neededTime[left: right]) - max(neededTime[left: right])
                left = right
            right += 1
        if right - left > 1:
            time += sum(neededTime[left: right]) - max(neededTime[left: right])
        return time